package com.dmall.rpc.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * 解码器
 * @author xiao1.wang
 * @date 2019-12-25 18:06:05
 */
public class RpcDecoder extends ByteToMessageDecoder {

    @Override
    public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        //将ByteBuf转换为byte[]
        int dataLength = in.readableBytes();
        byte[] data = new byte[dataLength];
        in.readBytes(data);
        in.clear();
        //将data转换成object ，就是拿到客户端发过来的RpcRequest对象
        Object obj = SerializationUtil.deserialize(data);
        
        // 将RpcRequest对象发往下一个inboundhandler去做业务调用
        out.add(obj);
    }
}
